package com.atguigu.myjuc.cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class CasDemo {


    private int A;
    public int getA() {
        return A;
    }

    public synchronized void setA() {
        ++A;
    }

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
        //atomicInteger.getAndIncrement();
        atomicInteger.incrementAndGet();
    }

    AtomicInteger atomicInteger = new AtomicInteger();


    /**
     *
     * 100个线程，每个线程加100次，求最后的number值
     *
     */



    public static void main(String[] args) throws InterruptedException {
        CasDemo casDemo = new CasDemo();
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 1; i <=100; i++) {
            new Thread(()->{
                for (int j = 1; j <= 100; j++) {
                    casDemo.setAtomicInteger();
                    casDemo.setA();
                }
                countDownLatch.countDown();

            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+"Atomic:"+casDemo.getAtomicInteger());
        System.out.println(Thread.currentThread().getName()+"\t"+"普通:"+casDemo.getA());


    }
}
